<template>
  <div>
    <el-container>
      <el-aside :style="{ width: isCollapse ? 'auto' : '200px' }">
        <navbar :menus="menus" :collapse="isCollapse"></navbar>
      </el-aside>

      <el-container>
        <el-header>
          <i
            class="el-icon-caret-bottom"
            :class="isCollapse ? 'collapseTransform' : ''"
            @click="handleCollapse"
          ></i>
          <h1>CMS后台界面</h1>
          <el-dropdown trigger="click">
            <span class="el-dropdown-link"
              ><b>admin</b>
              <i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item icon="el-icon-plus">用户信息</el-dropdown-item>
              <el-dropdown-item
                icon="el-icon-circle-plus-outline"
                @click.native="updatepass"
                >修改密码</el-dropdown-item
              >
              <!-- vue @click.native 原生点击事件：给vue组件绑定事件时候，必须加上native ，不然不会生效（监听根元素的原生事件，使用 .native 修饰符） -->
              <el-dropdown-item icon="el-icon-circle-check" @click.native="open"
                >注销</el-dropdown-item
              >
            </el-dropdown-menu>
          </el-dropdown>
        </el-header>

        <el-main>
          <router-view></router-view>
        </el-main>
        <el-footer>Footer</el-footer>
      </el-container>
    </el-container>

    <el-dialog title="修改密码" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="请输入原密码" :label-width="formLabelWidth">
          <el-input v-model="form.beforeusername" autocomplete="off" type="password"></el-input>
        </el-form-item>
        <el-form-item label="请输入密码" :label-width="formLabelWidth">
          <el-input v-model="form.password" autocomplete="off" type="password"></el-input>
        </el-form-item>
        <el-form-item label="请再次输入密码" :label-width="formLabelWidth">
          <el-input v-model="form.checkPass" autocomplete="off" type="password"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<style>
.el-header {
  background-color: #b3c0d1;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.el-icon-caret-bottom {
  display: flex;
  align-items: center;
}

.el-aside {
  color: #333;
}
.el-footer {
  background-color: #b3c0d1;
  color: #333;
  line-height: 60px;
}
.el-main {
  /* min-height: calc(100vh - 120px); */
  min-height: calc(100vh - 110px);
  padding: 0px !important;
}
.collapseTransform {
  transform: rotate(90deg);
}
</style>

<script>
import Navbar from "../components/Navbar";
import routes from "../router/routes";
import { modUserpass } from "../api/users";
export default {
  data() {
    return {
      isCollapse: false,
      dialogFormVisible: false,
      formLabelWidth: "120px",
      form: {
        beforeusername: "",
        password: "",
        checkPass: "",
      },
    };
  },
  components: {
    Navbar,
  },
  // 计算属性
  computed: {
    menus() {
      let arr = this.resolve(routes);
      return arr;
    },
    username() {
      var name = localStorage.getItem("name");
      return name;
    },
  },
  methods: {
    //提交新密码确认按钮事件
    confirm() {
      this.dialogFormVisible = false;
      var id = localStorage.getItem("id");
      if (
        this.form.password === this.form.checkPass &&
        this.form.password.length > 0 &&
        this.form.checkPass.length > 0
      ) {
        modUserpass(id, this.form)
          .then(({ data }) => {
            if (data.code === 1000) {
              console.log(data);
              this.$router.go(0);
            } else {
              this.$message({ type: "error", message: data.msg });
            }
          })
          .catch((err) => {
            console.log(err);
          });
      } else {
        this.$message({
          type: "error",
          message: "请验证二次输入的密码是否一致,且不能为空",
        });
      }
    },
    //修改密码框
    updatepass() {
      this.dialogFormVisible = true;
    },
    //注销
    open() {
      this.$confirm("此操作将退出主页面, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          // localStorage.removeItem("token");
          // localStorage.removeItem("refreshToken");
          // localStorage.removeItem("name");
          localStorage.clear();
          this.$router.push("/login");
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消注销",
          });
        });
    },

    resolve(arr, parentPath = "") {
      let newArr = [];
      //遍历数组当中的每一个元素
      arr.forEach((route) => {
        let obj = Object.assign({}, route);
        // 当上级路径存在，并且最后一位不为/，则后面添加 /
        parentPath =
          parentPath && parentPath.lastIndexOf("/") !== parentPath.length - 1
            ? parentPath + "/"
            : parentPath;
        //加上父级路径
        obj.path = parentPath + obj.path;
        // 判断有没有children属性 如有，那么需要再处理，如果没有则直接push到一个新数组中
        if (obj.children && obj.children.length > 0) {
          obj.children = this.resolve(obj.children, obj.path);
        }
        //    //判断hidden是否等于ture
        //   if (obj.meta.hidden == true) {
        //     //遍历children，使用Object.assign合并对象，在返回数组
        //     obj.children.forEach((item) => {
        //       let tmp = Object.assign({}, item);
        //       newArr.push(tmp);
        //     });
        //   } else {
        //     //ishidden不等于ture则添加到数组
        //     newArr.push(obj);
        //   }
        // });
        //如果当前路由元信息有hidden信息，那么就将其子节点提升上来
        if (obj.meta && obj.meta.hidden) {
          if (obj.children && obj.children.length > 0) {
            obj.children.forEach((item) => {
              let tmp = Object.assign({}, item);
              newArr.push(tmp);
            });
          }
        } else {
          newArr.push(obj);
        }
      });
      return newArr;
    },
    handleCollapse() {
      this.isCollapse = !this.isCollapse;
    },
  },
};
</script>